package com.qinge.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinge.blog.entity.BlogCategoryExample;
import com.qinge.blog.utils.CurrentUserUtil;
import com.qinge.blog.vo.paging.PagingReqVO;
import org.springframework.stereotype.Service;
import com.qinge.blog.dao.BlogCategoryDao;
import com.qinge.blog.service.BlogCategoryService;
import com.qinge.blog.entity.BlogCategory;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author QDW
 * @date 2022-05-02 23:38:51
 * @description BlogCategory service层实现类
 */
@Service
public class BlogCategoryServiceImpl   implements BlogCategoryService {

    @Resource
    BlogCategoryDao categoryDao;

    @Override
    public void add(BlogCategory... blogCategories) {
        for (BlogCategory category:blogCategories) {
            category.setUserId(CurrentUserUtil.getUserId());
        }

        categoryDao.batchInsert(Arrays.asList(blogCategories));
    }

    @Override
    public void addByList(List<BlogCategory> blogCategories) {
        for (BlogCategory category:blogCategories) {
            category.setUserId(CurrentUserUtil.getUserId());
        }

        categoryDao.batchInsert(blogCategories);
    }

    @Override
    public void removeById(String id) {
        BlogCategoryExample example=new BlogCategoryExample();
        BlogCategoryExample.Criteria criteria=example.createCriteria();


        criteria.andIdEqualTo(id);

        if(!CurrentUserUtil.isAdmin()){
            criteria.andUserIdEqualTo(CurrentUserUtil.getUserId());
        }

        categoryDao.deleteByExample(example);

    }

    @Override
    public void removeBatchByIds(List<String> ids) {
        BlogCategoryExample example=new BlogCategoryExample();
        BlogCategoryExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        if(!CurrentUserUtil.isAdmin()){
            criteria.andUserIdEqualTo(CurrentUserUtil.getUserId());
        }

        categoryDao.deleteByExample(example);
    }

    @Override
    public void update(BlogCategory category) {
        categoryDao.upsert(category);
    }

    @Override
    public void upsert(BlogCategory category) {
        categoryDao.upsert(category);
    }

    @Override
    public BlogCategory findById(String id) {
        return categoryDao.selectByPrimaryKey(id);
    }

    @Override
    public List<BlogCategory> findByIds(List<String> ids) {
        BlogCategoryExample example=new BlogCategoryExample();
        BlogCategoryExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        if(!CurrentUserUtil.isAdmin()){
            criteria.andUserIdEqualTo(CurrentUserUtil.getUserId());
        }

        return categoryDao.selectByExample(example);
    }

    @Override
    public PageInfo<BlogCategory> findAll() {
        return paging(new PagingReqVO<>());
    }

    @Override
    public PageInfo<BlogCategory> findAll(PagingReqVO<BlogCategory> pagingReqVO) {
        return paging(pagingReqVO);
    }

    @Override
    public PageInfo<BlogCategory> paging(PagingReqVO<BlogCategory> pagingReqVO) {
        PageHelper.startPage(pagingReqVO);

        BlogCategoryExample example=new BlogCategoryExample();

        BlogCategoryExample.Criteria criteria=example.createCriteria();

        if(!CurrentUserUtil.isAdmin()){
            criteria.andUserIdEqualTo(CurrentUserUtil.getUserId());
        }

        List<BlogCategory> list=categoryDao.selectByExample(example);

        return new PageInfo<>(list);
    }
}

